<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospset.hosname"/>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospset.hoscode" :readonly="readonly"/>
      </el-form-item>
      <el-form-item label="api地址">
        <el-input v-model="hospset.apiUrl"/>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospset.contactsName"/>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="hospset.contactsPhone"/>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import hospset from '@/api/yygh/hospset'
export default{
  data(){
    return{
      hospset:{},
      readonly:false,//默认可以编辑
      saveBtnDisabled:false//保存按钮是否禁用
    }
  },
  methods:{
    saveOrUpdate(){
      this.saveBtnDisabled = true
      if(!this.hospset.id){
        this.saveData()
      }else{
        this.updateData()
      }
    },

    //保存
    saveData(){
      hospset.save(this.hospset).then(response =>{
        this.$message({
          type:'success',
          message:'保存成功'
        })
        //注意:路由跳转时,使用$router
        this.$router.push({
          path:'/yygh/hospset/list'
        })
      })
    },

    fetchDataById(id){
      hospset.getById(id).then(response=>{
        this.hospset = response.data.item
      })
    },
    updateData(){
      this.saveBtnDisabled = true
      hospset.updateById(this.hospset)
      .then(response=>{
        return this.$message({
          type:'success',
          message:'修改成功'
        })
      }).then(response=>{
        this.$router.push({
          path:'/yygh/hospset/list'
        })
      })
    }
  },
  //注意：获取路由参数，使用的是 $route
  created(){
    if(this.$route.params && this.$route.params.id){
      const id = this.$route.params.id
      this.fetchDataById(id),
      this.readonly = true
    }
  }
}
</script>